<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="800px"
    append-to-body
    :close-on-click-modal="false"
    @close="closeModal"
  >
    <el-form ref="form" :model="form" :show-message="false" label-width="150px">
      <el-form-item label="体质名称" prop="constitutionaName" required>
        <el-input
          v-model.trim="form.constitutionaName"
          placeholder="请输入体质名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="体质类型" prop="constitutionaType">
        <el-select
          v-model="form.constitutionaType"
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option label="正常体质" value="正常体质"></el-option>
          <el-option label="偏颇体质" value="偏颇体质"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="总体特征" prop="totalFeature">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 5 }"
          placeholder="请输入总体特征"
          v-model="form.totalFeature"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="形体特征" prop="formFeature">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 5 }"
          placeholder="请输入形体特征"
          v-model="form.formFeature"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="常见表现" prop="performance">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 5 }"
          placeholder="请输入常见表现"
          v-model="form.performance"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="心理特征" prop="psychologyFeature">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 5 }"
          placeholder="请输入心理特征"
          v-model="form.psychologyFeature"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="发病倾向" prop="pathogenicTendency">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 5 }"
          placeholder="请输入发病倾向"
          v-model="form.pathogenicTendency"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="对外界环境适应能力" prop="adaptability">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 5 }"
          placeholder="请输入对外界环境适应能力"
          v-model="form.adaptability"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="调节方法" prop="adjustment">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 5 }"
          placeholder="请输入调节方法"
          v-model="form.adjustment"
        >
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  addConstitution,
  updateConstitution,
  getConstitutionDetails,
} from "@/api/saas/tcm.js";

export default {
  data() {
    return {
      title: "新建体质",
      open: false,
      form: {
        id: undefined,
        constitutionaName: "",
        constitutionaType: "",
        totalFeature: "",
        formFeature: "",
        psychologyFeature: "",
        pathogenicTendency: "",
        adaptability: "",
        performance: "",
        adjustment: "",
        status: 1,
      },
    };
  },
  methods: {
    openModal(id) {
      this.title = id ? "编辑体质" : "新建体质";
      this.open = true;
      this.form.id = id || undefined;
      this.form.id && this.getData();
    },
    closeModal() {
      this.$refs["form"].resetFields();
      Object.assign(this.$data, this.$options.data()); // 存在自定义校验规则不能重置data
    },
    // 获取详情
    getData() {
      getConstitutionDetails(this.form.id).then((res) => {
        this.form = res.data;
      });
    },
    submitForm() {
      const that = this;
      that.$refs["form"].validate(async (valid) => {
        if (valid) {
          if (that.form.id) {
            await updateConstitution(that.form);
          } else {
            await addConstitution(that.form);
          }
          that.$message.success(that.form.id ? "修改成功" : "新增成功");
          that.$emit("refresh");
          that.closeModal();
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
